package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DemoBatch {
    public static void main(String[] args) throws Exception {
        //1、加载驱动
        Class.forName("com.mysql.jdbc.Driver");
//        String url = "jdbc:mysql://localhost:3306/fruitdb?characterEncoding=utf-8&rewriteBatchedStatements=true&useSSL=false";
        String url = "jdbc:mysql://125.74.233.192:3306/fruitdb?characterEncoding=utf-8&rewriteBatchedStatements=true&useSSL=false";
        String user = "root";
        String password = "123456123";
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        String sql = "insert into t_fruit values(0,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < 10; i++) {
            preparedStatement.setString(1,"西瓜"+i);
            preparedStatement.setInt(2,15+i);
            preparedStatement.setInt(3,10);
            preparedStatement.setString(4,"这瓜保熟吗？");
            preparedStatement.addBatch();
        }
        int[] ints = preparedStatement.executeBatch();
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);

        }
        preparedStatement.close();
        connection.close();

    }
}
